﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.Commands;
using Microsoft.Practices.CompositeUI.BuilderStrategies;
using Microsoft.Practices.CompositeUI.UIElements;
using Microsoft.Practices.ObjectBuilder;

namespace WPFCABShell.$safeprojectname$
{
	/// <summary>
	/// A <see cref="WorkItem"/> that can be used for unit testing.
	/// </summary>
	public sealed class TestableWorkItem : WorkItem
	{
		public Builder Builder
		{
			get
			{
				return InnerBuilder;
			}
		}

		public IReadWriteLocator Locator
		{
			get
			{
				return InnerLocator;
			}
		}

		public TestableWorkItem()
		{
			InitializeRootWorkItem(CreateBuilder());
			AddServices();
		}

		public TestableWorkItem(WorkItem parent)
		{
			Parent = parent;
			InitializeWorkItem();
			AddServices();
		}

		private Builder CreateBuilder()
		{
			Builder builder = new Builder();

			builder.Strategies.AddNew<EventBrokerStrategy>(BuilderStage.Initialization);
			builder.Strategies.AddNew<CommandStrategy>(BuilderStage.Initialization);
			builder.Strategies.AddNew<ObjectBuiltNotificationStrategy>(BuilderStage.PostInitialization);

			builder.Policies.SetDefault<ObjectBuiltNotificationPolicy>(new ObjectBuiltNotificationPolicy());
			builder.Policies.SetDefault<ISingletonPolicy>(new SingletonPolicy(true));

			return builder;
		}

		private void AddServices()
		{
			Services.AddNew<CommandAdapterMapService, ICommandAdapterMapService>();
			Services.AddNew<UIElementAdapterFactoryCatalog, IUIElementAdapterFactoryCatalog>();
		}
	}
}
